/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.commonutils.redis;

import com.seari.commonutils.redis.ex.JedisNotInitializeException;

import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 *
 * @author Rainbow
 */
public class JedisUtilProxy
{

    public static boolean useSentinel = false;

    public static Jedis getJedis() throws JedisNotInitializeException
    {
        Jedis jedis = null;
        if (JedisPoolUtil.jedisPool == null && JedisSentinelUtil.jedisPool == null)
        {
            throw new JedisNotInitializeException();
        } else
        {
            if (!useSentinel)
            {
                jedis = JedisPoolUtil.getJedis();
            } else
            {
                jedis = JedisSentinelUtil.getJedis();
            }
        }
        return jedis;
    }
    
    public static String getStatus()
    {
        return "Idle"+JedisPoolUtil.jedisPool.getNumIdle() + "\r\n" +
                "Active" + JedisPoolUtil.jedisPool.getNumActive() + "\r\n" +
                "Waiters" + JedisPoolUtil.jedisPool.getNumWaiters() + "\r\n" +
                "isClosed" + JedisPoolUtil.jedisPool.isClosed();
    }
    
    

    public static void close(Jedis jedis)
    {
        if (!useSentinel)
        {
            JedisPoolUtil.close(jedis);
        } else
        {
            JedisSentinelUtil.close(jedis);
        }
    }
    
    //非哨兵模式初始化
    public static void initJedisPoolUtil(String ip ,int port)
    {
        useSentinel = false;
        JedisPoolUtil.init(ip, port);
    }
    
    //哨兵模式初始化
    public static void initJedisSentinelUtil(Set<String> sentinels, String masterName)
    {
        useSentinel = true;
        JedisSentinelUtil.init(sentinels, masterName);
    }
    
    public static JedisPool getJedisPool()
    {
        if (!useSentinel)
        {
            return JedisPoolUtil.jedisPool;
        }
        else{
            return null;
        }
    }
}
